package com.fadadianqi.logistics.admin;

import com.fadadianqi.logistics.admin.email.EmailService;
import com.fadadianqi.search.IndexJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ScheduledJobs {

    private static final Log log = LogFactory.getLog(ScheduledJobs.class);

    @Autowired
    private IndexJob indexJob;

    @Autowired
    private DataBackup dataBackup;

    @Autowired
    private EmailService emailService;

    public void dbbackup() {
        try {
            log.info("dbbackup run on " + new Date());
            dataBackup.backup();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
        }
    }

    public void send() {
        try {
            log.info("send run on " + new Date());
            emailService.job();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
        }
    }

    public void bufferIndex() {
        log.info("bufferIndex run on " + new Date());
        indexJob.run();
    }

    public void rebuild() {
        log.info("index rebuild start..." + new Date());
        try {
            indexJob.rebuild();
        } catch (Exception e) {
            log.error(e.toString());
        }
        log.info("index rebuild finished!!!" + new Date());
    }
}
